//
//  MainViewController.swift
//  MyWeiBo
//
//  Created by 张玉 on 15/9/12.
//  Copyright © 2015年 ZY. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
       addChildViewControllers()
  
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
         // 此时 tabBarButton 都已经创建 不能再viewDidLoad 调用
        setupComposedButton()
        
    }
    
    /// 设置撰写按钮位置
    private func setupComposedButton(){
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composeButton.frame = CGRectOffset(rect, 2 * w, 0)
        
    }
    
    
    /// 运行循环监听到事件后，向 VC 发送消息，动态执行 方法，因此不能设置为 private
    func clickComposedButton() {
        // 根据用户是否登录，来决定是否能够显示发布微博的界面
        let vc = UserAccount.userLogon ? ComposeViewController() : OauthViewController()
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    }
    //添加所有的控制器
    
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }

    //重载addChildViewController(childController: UIViewController) 方法 不要参数
    private func addChildViewController(vc:UIViewController, title:String, imageName:String ) {
        
        
        //实例化一个nav 设置他的根控制器是 vcHomeTableViewController
        let nav = UINavigationController(rootViewController: vc)
        
        //title属性从外向内设置
        vc.title = title
       //设置tabbar的背景图片
        vc.tabBarItem.image = UIImage(named: imageName)
        
        
        
        
        //main的TabBarController 添加nav
        addChildViewController(nav)
    
    }
    //MARK:- 懒加载btn
    private lazy var composeButton:UIButton = {
    
        let btn = UIButton()
        
    //设置btn的背景图片和 image
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
  
    
    }()
    
    
    
}
